package xiangya.bysj.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.taobao.api.TaobaoClient;
import com.taobao.api.domain.CheckVerCodeRequest;
import com.taobao.api.domain.SendMessageRequest;
import com.taobao.api.domain.SendMessageResult;
import com.taobao.api.domain.SendVerCodeRequest;
import com.taobao.api.request.OpenSmsCheckvercodeRequest;
import com.taobao.api.request.OpenSmsSendmsgRequest;
import com.taobao.api.request.OpenSmsSendvercodeRequest;
import com.taobao.api.response.OpenSmsCheckvercodeResponse;
import com.taobao.api.response.OpenSmsSendmsgResponse;
import com.taobao.api.response.OpenSmsSendvercodeResponse;

import xiangya.bysj.service.interf.SMSServiceInterface;
import xiangya.bysj.util.PropertyUtil;
import xiangya.bysj.util.StringUtil;

@Service("SMSService")
public class SMSServiceImpl implements SMSServiceInterface {

	/**
	 * 验证码超时时间 key
	 */
	private final static String EXPIRE_TIME = "expireTime";
	/**
	 * 手机发送验证码的次数限制 key
	 */
	private final static String MOBILE_LIMIT = "mobileLimit";
	/**
	 * 限制时间 单位秒 key
	 */
	private final static String MOBILE_LIMIT_IN_TIME = "mobileLimitInTime";
	/**
	 * 签名id key
	 */
	private final static String SIGNATURE_ID = "signatureId";
	/**
	 * 验证失败次数 key
	 */
	private final static String CHECK_FAIL_LIMIT = "checkFailLimit";
	/**
	 * 验证成功次数 key
	 */
	private final static String CHECK_SUCCESS_LIMIT = "checkSuccessLimit";
	/**
	 * 验证码长度 key
	 */
	private final static String VERCODE_LENGTH = "verCodeLength";
	/**
	 * 验证码短信模板id key
	 */
	private final static String VERCODE_TEMP_ID = "verCodeTempId";

	@Resource(name = "aliClient")
	private TaobaoClient smsClient;

	/**
	 * 发送验证码的请求对象
	 */
	private OpenSmsSendvercodeRequest vercodeSendReq;
	/**
	 * 发送验证码的响应对象
	 */
	private OpenSmsSendvercodeResponse vercodeSendRes;
	/**
	 * 检查验证码的请求对象
	 */
	private OpenSmsCheckvercodeRequest vercodeCheckReq;
	/**
	 * 检查验证码的响应对象
	 */
	private OpenSmsCheckvercodeResponse vercodeCheckRes;
	/**
	 * 发送系统短信的请求对象
	 */
	private OpenSmsSendmsgRequest msgSendReq;
	/**
	 * 发送系统短信的响应对象
	 */
	private OpenSmsSendmsgResponse msgSendRes;

	/**
	 * 发送验证码
	 */
	@Override
	public String sendVercode(String phone) throws Exception {
		vercodeSendReq = new OpenSmsSendvercodeRequest();
		SendVerCodeRequest vercode = new SendVerCodeRequest();
		vercode.setExpireTime(Long.parseLong(PropertyUtil.getProperties().getProperty(EXPIRE_TIME)));
		vercode.setMobileLimit(Long.parseLong(PropertyUtil.getProperties().getProperty(MOBILE_LIMIT)));
		vercode.setMobileLimitInTime(Long.parseLong(PropertyUtil.getProperties().getProperty(MOBILE_LIMIT_IN_TIME)));
		vercode.setTemplateId(Long.parseLong(PropertyUtil.getProperties().getProperty(VERCODE_TEMP_ID)));
		vercode.setSignatureId(Long.parseLong(PropertyUtil.getProperties().getProperty(SIGNATURE_ID)));
		vercode.setVerCodeLength(Long.parseLong(PropertyUtil.getProperties().getProperty(VERCODE_LENGTH)));
		vercode.setMobile(phone);
		vercodeSendReq.setSendVerCodeRequest(vercode);
		vercodeSendRes = smsClient.execute(vercodeSendReq);
		if (!vercodeSendRes.isSuccess() || !vercodeSendRes.getResult().getSuccessful()) {
			return StringUtil.RES_FAIL;
		} else {
			return StringUtil.RES_SUCCESS;
		}
	}

	/**
	 * 验证 验证码
	 */
	@Override
	public String checkVerCode(String phone, String code) throws Exception {
		vercodeCheckReq = new OpenSmsCheckvercodeRequest();
		CheckVerCodeRequest vercode = new CheckVerCodeRequest();
		vercode.setCheckFailLimit(Long.parseLong(PropertyUtil.getProperties().getProperty(CHECK_FAIL_LIMIT)));
		vercode.setCheckSuccessLimit(Long.parseLong(PropertyUtil.getProperties().getProperty(CHECK_SUCCESS_LIMIT)));
		vercode.setMobile(phone);
		vercode.setVerCode(code);
		vercodeCheckReq.setCheckVerCodeRequest(vercode);
		vercodeCheckRes = smsClient.execute(vercodeCheckReq);
		if (!vercodeCheckRes.isSuccess() || !vercodeCheckRes.getResult().getSuccessful()) {
			return StringUtil.RES_FAIL;
		} else {
			return StringUtil.RES_SUCCESS;
		}
	}

	/**
	 * 发送系统短信
	 */
	@Override
	public void sendSms(String phone, String sendJson, long tempId) throws Exception {
		msgSendReq = new OpenSmsSendmsgRequest();
		SendMessageRequest msg = new SendMessageRequest();
		msg.setSignatureId(Long.parseLong(PropertyUtil.getProperties().getProperty(SIGNATURE_ID)));
		msg.setTemplateId(tempId);
		msg.setContextString(sendJson);
		msg.setMobile(phone);
		msgSendReq.setSendMessageRequest(msg);
		msgSendRes = smsClient.execute(msgSendReq);
	}
}
